package com.bag;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ZipUtil;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import java.util.stream.Collectors;

/**
 * @Author longhj
 * @Description TODO
 * @Date 2021/09/18 星期六 21:29
 */
public class BagPack {


    private static final String INFO_TXT = "info.txt";

    public static void main(String[] args) throws IOException {
        BagPack bagPack = new BagPack();
        String propertyFilePath = bagPack.getPropertyFilePath();
        // String propertyFilePath = "D:\\workHome\\Jack_Idea\\bagpack\\test";
        //获取样本info.txt
        File fileinfo = bagPack.readInfoFile(propertyFilePath, "infodemo.txt");
        String fileInfoStr = FileUtil.readString(fileinfo, "utf-8");
        //读取html后缀的文件
        List<File> readfile = bagPack.readfile(propertyFilePath, ".*"+args[0]);
        // deleteFile(propertyFilePath + "\\info.txt");
        readfile.stream().forEach(f -> {
            String fileZip = f.getPath().replaceAll("\\..*", ".zip");
            File zip = new File(fileZip);
            String name = f.getName();
            String s2 = fileInfoStr.replaceAll("\\{BASE_FILE_NAME}", name);
            File file2 = FileUtil.writeString(s2, new File("info.txt"), "utf-8");
            System.out.println("生成文件:"+zip.getName());
            ZipUtil.zip(zip, false, f,file2);
        });
        deleteFile(propertyFilePath + "\\info.txt");

    }
    /**
     * 删除单个文件
     * @param   sPath    被删除文件的文件名
     * @return 单个文件删除成功返回true，否则返回false
     */
    public static boolean deleteFile(String sPath) {
        boolean flag = false;
        File file = new File(sPath);
        // 路径为文件且不为空则进行删除
        if (file.isFile() && file.exists()) {
            file.delete();
            flag = true;
        }
        return flag;
    }
    /**
     * 依据文件名筛选符合的文件
     *
     * @param listFile
     * @param pattern
     * @return
     */
    private List<File> getSuitFile(List<File> listFile, final String pattern) {
        return listFile.stream().filter(file -> {
            if (file.isDirectory()) {
                return false;
            }
            if (file.getName().matches(pattern)) {
                return true;
            }
            return false;
        }).collect(Collectors.toList());
    }


    /**
     * 得到jar配置文件路径
     */
    private String getPropertyFilePath() throws IOException {
        String jarWholePath = this.getClass().getProtectionDomain().getCodeSource().getLocation().getFile();

        jarWholePath = java.net.URLDecoder.decode(jarWholePath, "UTF-8");

        String jarPath = new File(jarWholePath).getParentFile().getAbsolutePath() + "/";
        return jarPath;
    }

    /**
     * 读取某个文件夹下的所有文件
     */
    public File readInfoFile(String filepath, String name) {
        File file = new File(filepath);
        if (!file.isDirectory()) {
            System.out.println("该路径下是文件，不符合要求");
        } else if (file.isDirectory()) {
            System.out.println(("读取信息文件"));
            String[] filelist = file.list();
            for (int i = 0; i < filelist.length; i++) {
                if (filelist[i].equals(name)) {
                    return new File(filepath+"\\"+filelist[i]);
                }
            }
        }
        return null;
    }

    /**
     * 读取某个文件夹下的所有文件
     */
    public List<File> readfile(String filepath, String pattern) {
        ArrayList<File> fileArrayList = new ArrayList<>();
        File file = new File(filepath);
        if (!file.isDirectory()) {
            System.out.println("jar放的位置是文件");
        } else if (file.isDirectory()) {
            String[] filelist = file.list();
            for (int i = 0; i < filelist.length; i++) {
                if (StrUtil.isBlank(checkIsNeedFile(filelist[i], pattern)) && !filelist[i].equals(INFO_TXT)) {
                    continue;
                }
                File readfile = new File(filepath + "\\" + filelist[i]);
                if (!readfile.isDirectory()) {
                    System.out.println("读取到文件:" + readfile.getName());
                    fileArrayList.add(readfile);
                } else if (readfile.isDirectory()) {
                    continue;
                }
            }
        }
        return fileArrayList;
    }

    /**
     * 输入文件名，判断是否是符合正则的名的文件，是返回文件名
     *
     * @param fileName
     * @param pattern
     * @return
     */
    public String checkIsNeedFile(String fileName, String pattern) {
        if (fileName.matches(pattern)) {
            return fileName;
        }
        return null;
    }


}
